import { createStore } from "redux";

// 1.state的初始值
const initState = {
  count: 0,
};

// 2.声明action creator 并导出,payload是派发action时传进来的数据
// action是个对象, action creator是一个函数
export const updateCount = function (payload) {
  // 此函数返回一个action
  return {
    type: "UPDATE_COUNT",
    payload,
  };
};

// 3.声明reducer (类似vuex里的mutation)
const reducer = function (state = initState, action) {
  let { type, payload } = action;
  switch (type) {
    case "UPDATE_COUNT":
      return {
        ...state,
        count: state.count + payload,
      };
    default:
      return state;
  }
};

let store = createStore(reducer);
export default store;
